﻿#include "tcp_server.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("P2P Server");//设置标题
    setFixedSize(1080, 700); // 设置窗口大小
    this->setStyleSheet("background-color: rgb(198,252,252);");//设置背景颜色

//    LogLable = new QLabel("操作日志");
//    LogLable->setStyleSheet("color: black; font-size: 20px; font-weight: bold;");
//    ContentListWidget = new QListWidget;
//    ContentListWidget->setStyleSheet("background-color: rgb(235,255,255)");

    myWidget = new QWidget;
    PortLabel = new QLabel("端口:");
    PortLabel->setStyleSheet("font-weight:bold;");
    PortLineEdit = new QLineEdit;
    PortLineEdit->setStyleSheet("background-color: rgb(235,255,255)");
    CreateBtn = new QPushButton("打开服务器");
    CreateBtn->setStyleSheet("background-color: rgb(135,243,246)");
    downloadBtn = new QPushButton("下载资源");
    downloadBtn->setStyleSheet("background-color: rgb(135,243,246)");

    UserLable = new QLabel("在线用户");
    UserLable->setStyleSheet("color: black; font-size: 20px; font-weight: bold;");
    resourceTree = new QTreeWidget();
    resourceTree->setStyleSheet("background-color: rgb(235,255,255)");
    resourceTree->header()->setVisible(false);//不显示headerlabel
    resourceTree->clear();

    mainLayout = new QGridLayout();

//    mainLayout->addWidget(LogLable,0,0);
//    mainLayout->addWidget(ContentListWidget,1,0,1,2);
    mainLayout->addWidget(PortLabel,2,0);
    mainLayout->addWidget(PortLineEdit,2,1);
    mainLayout->addWidget(CreateBtn,3,0,1,2);
    mainLayout->addWidget(downloadBtn,4,0,1,2);
    mainLayout->addWidget(UserLable,0,0);
    mainLayout->addWidget(resourceTree,1,0,1,2);
    myWidget->setLayout(mainLayout);
    setCentralWidget(myWidget);

    //完成界面的设计

    port = 8010;//默认端口设置为8010
    PortLineEdit->setText(QString::number(port));//更改编辑框值
    connect(CreateBtn,SIGNAL(clicked()),this,SLOT(slotCreateServer()));//将打开服务器按钮与创建服务器绑定
    connect(downloadBtn, &QPushButton::clicked, this, [=](){
        QString url = QInputDialog::getText(0, "新建下载", "请输入下载链接");
        if(url.isEmpty())return 0;
        QString filename = url.split("/")[url.split("/").length() - 1];
        qDebug()<<"filename:"<<filename;
        QString file = QFileDialog::getSaveFileName(0, "保存文件", filename);
        if(file.isEmpty())return 0;
        file = QFileInfo(file).absoluteFilePath();
        mainControl = new DownloadMainControl(0, url, file);
        mainControl->show();
    });

    server = 0;
}

void MainWindow::slotCreateServer(){
    server = new Server(this, port);//创建Server服务器对象，并与数据库进行绑定
    server->db = QSqlDatabase::addDatabase("QMYSQL");
    server->db.setHostName("localhost");
    server->db.setDatabaseName("p2p");
    server->db.setUserName("root");
    server->db.setPassword("111111");
    if (!server->db.open()) {//连接数据库失败，输出错误信息
        QMessageBox::critical(0, QObject::tr("无法打开数据库"),
        "无法创建数据库连接！ ", QMessageBox::Cancel);
        return;
    }
    connect(server,SIGNAL(UpdateServer(QString,int,Server::MsgKind)),this,SLOT(UpdateServer(QString,int,Server::MsgKind)));
    CreateBtn->setEnabled(false);//将打开服务器按钮设置为不可用

}

void MainWindow::UpdateServer(QString msg, int length, Server::MsgKind flag){
    switch (flag) {
    case Server::UpdateMsg:
    {
        ContentListWidget->addItem(msg.left(length));
        break;
    }
    case Server::UPDATEMETA:
    {
        ContentListWidget->addItem(msg + ": update meta");
        break;
    }
    case Server::UpdateName:
    {
        QTreeWidgetItem *item_name = new QTreeWidgetItem(resourceTree);
        item_name->setText(0,msg);
        break;
    }
    case Server::RemoveName:
    {
        QPoint p(length,0);
        resourceTree->removeItemWidget(resourceTree->itemAt(p),0);
        delete resourceTree->itemAt(p);
        break;
    }
    }
}

MainWindow::~MainWindow()
{
    delete PortLabel;
    delete PortLineEdit;
    delete CreateBtn;
    delete ContentListWidget;
    resourceTree->clear();
    delete resourceTree;
    delete mainLayout;
    delete myWidget;
    if(server != 0){
        server->db.close();
        delete server;
    }
}
